package com.mall.utils;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

//
public class ClassToMap {
    //获取一个类的所有属性，并返回一个Map
    public static <T> Map<String, Object> getFieldMap(T obj) {
        Map<String, Object> map = new HashMap<>();
        Class clazz = obj.getClass();
        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {
            field.setAccessible(true);
            try {
                map.put(field.getName(), field.get(obj));
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        return map;
    }
}
